//
//  LXChildPopupViewController.m
//  TestChildVC
//
//  Created by Livespro on 2017/6/30.
//  Copyright © 2017年 FZ. All rights reserved.
//

#import "LXChildPopupViewController.h"


CGRect AdjustAnimation(CGRect frame,LXChildPopupAnimation animation){
    
    if (animation == LXChildPopupAnimationFromDown) {
        
        frame.origin.y = [UIScreen mainScreen].bounds.size.height;
        
    }else if (animation == LXChildPopupAnimationFromRight){
        
        frame.origin.x = [UIScreen mainScreen].bounds.size.width;
    }
    
    return frame;
}

@interface LXChildPopupViewController ()

@property (nonatomic, assign)  LXChildPopupAnimation currentAnimation;

@property (nonatomic, strong)  UIView * overlayer;

@end

@implementation LXChildPopupViewController

- (instancetype)initWithAnimationType:(LXChildPopupAnimation)animationType Frame:(CGRect)viewFrame;{
   
    self = [super init];
    if (self) {
        
        self.currentAnimation = animationType;
        
        self.view.frame = viewFrame;
    }
    return self;
}


- (UIView *)overlayer{
    if (!_overlayer) {
        
        _overlayer = [[UIView alloc] initWithFrame:self.parentViewController.view.bounds];
        _overlayer.backgroundColor = [UIColor colorWithWhite:0 alpha:.4];
        
        UIButton * dismissButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [dismissButton addTarget:self action:@selector(missAction) forControlEvents:UIControlEventTouchUpInside];
        dismissButton.backgroundColor = [UIColor clearColor];
        dismissButton.frame = self.parentViewController.view.bounds;
        [_overlayer addSubview:dismissButton];
    }
    return _overlayer;
}


- (void)showAction;{
    
    [self.parentViewController.view addSubview:self.overlayer];
    [self.parentViewController.view addSubview:self.view];
    
    self.overlayer.alpha = 0;
    CGRect frame = self.view.frame;
    
    frame = AdjustAnimation(frame, _currentAnimation);
    
    self.view.frame = frame;
    
    [UIView animateWithDuration:.3 animations:^{
        
        self.view.frame = CGRectMake(0, [UIScreen mainScreen].bounds.size.height - frame.size.height, [UIScreen mainScreen].bounds.size.width, frame.size.height);
        
        self.overlayer.alpha = 1;
        
    }];
    
    
}

- (void)missAction;{
    
    CGRect frame = self.view.frame;
    
    frame = AdjustAnimation(frame, _currentAnimation);
    
    [UIView animateWithDuration:.3 animations:^{
        
        self.overlayer.alpha = 0;
        
        self.view.frame = frame;
        
    } completion:^(BOOL finished) {
        
        [self.overlayer removeFromSuperview];
        
        [self.view removeFromSuperview];
        
    }];
}

@end
